Type.registerNamespace("CodeStory");


CodeStory.FileInfo = function(key,urls){
	this.Urls = urls;
	this.Key = key;
	this.Loaded = false;
	this.pendingUrls = function(u){
		var au = u.constructor === Array ? u : [u];
	}
}

CodeStory.Editor = function(){
};
CodeStory.Editor.ClientScripts = [];

CodeStory.Editor.prototype = {
	Commands:new Object(),
	/*
	 * @name[String]:element name of the iframe container
	 */
	Initialize:function(name){
		
	},
	RegisterCommand:function(command){
		this.Commands[command.CommandName] = command;
	}
};

/*
 * method:CodeStory.Editor.Invoke
 * parameters:
 * @name[String]:The key registered for the javascript file
 * @method[String or Function pointer]:The method to invoke;
 * 
 */
CodeStory.Editor.Invoke = function(){
	if (arguments.length < 2) 
		return;
	var name = arguments[0];
	var method = arguments[1];
	var args = [];
	for (var i = 2; i < arguments.length; i++) {
		args[i - 2] = arguments[i];
	}
	var obj;
	try {
		obj = eval(method);
	}catch(e){}
	if (obj != undefined) {
		return obj.call(this, args);
	}
	var scope;
	if (method.indexOf(".") > -1){
		var index = method.lastIndexOf(".");
		scope = method.substr(0,index);
	}
	for(var k = 0; k < this.ClientScripts.length; k++){
		var fi = this.ClientScripts[k];
		if (fi.Key === name){
			LazyLoad.loadOnce(fi.Urls,method,args,scope);
			fi.Loaded = true;
			break;
		}
	}
};

CodeStory.Editor.RegisterClientScript = function(key,urls){
	for (var i = 0; i < this.ClientScripts.length; i++) {
		var f = this.ClientScripts[i];
		if (f.Key === key) {
			f.pendingUrls(urls);
			return;
		}
	}
	var f = new CodeStory.FileInfo(key,urls);
	this.ClientScripts.push(f);
};


CodeStory.Editor.registerClass("CodeStory.Editor");
